﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using MySpace.Data;
using DataBaseTable;
public partial class Manage_Golbal_AdminListaspx : System.Web.UI.Page
{
    private static int nowpages;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            nowpages = 1;
            DataBd();
        }
    }
    protected void DataBd()
    {
        string sql = "select * from admin where id>1 order by id desc";
        DBHandler bdh = GetHandler.Init();
        bdh.CommandText = sql;
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = bdh.ExecuteDataTable().DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 5;
        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == ps.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        Label1.Text = nowpages.ToString();
        Label2.Text = ps.PageCount.ToString();
        tpagesizes.Text = ps.PageSize.ToString();
        pagesum.Text = ps.DataSourceCount.ToString();
        ps.CurrentPageIndex = nowpages - 1;//获取当前页码
        Repeater1.DataSource = ps;
        Repeater1.DataBind();
    }
    protected void Repeater1_RepeaterCommand(object sender, RepeaterCommandEventArgs e)
    {
        AdminTable ad = new AdminTable(Convert.ToInt32(((HiddenField)e.Item.FindControl("Hiddenid")).Value));
        if (e.CommandName == "Edit")
        {
            ad.UserName = ((TextBox)e.Item.FindControl("UserName")).Text;
            ad.TureName = ((TextBox)e.Item.FindControl("TrueName")).Text;
            if (!string.IsNullOrEmpty(((TextBox)e.Item.FindControl("UserPassWord")).Text.Trim()))
            {
                ad.UserPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(((TextBox)e.Item.FindControl("UserPassWord")).Text.Trim(), "MD5").ToLower();
            }
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('普通管理员更新成功');</script>");
            ad.Update();
        }
        else if (e.CommandName == "Del")
        {
            ad.Delete();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('普通官员删除成功');</script>");
            DataBd();
        }
    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        DataBd();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        DataBd();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        DataBd();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(Label2.Text);
        DataBd();
    }
}